package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.BriefAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.supervision.dto.SpAuditDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpHelpResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpBriefPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.service.SpAuditService;
import com.engine4cloud.tdf.supervision.service.SpHelpAuditService;
import com.engine4cloud.tdf.supervision.service.SpHelpService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SpHelpAuditServiceImpl implements SpHelpAuditService {
    @Resource
    private SpAuditService auditService;
    @Resource
    private SpHelpService helpService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(SpAuditDTO dto) {
        SpHelpResultDTO old = Optional.ofNullable(helpService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        helpService.changeAction(new SpBriefPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), BriefAuditActionStatusEnum.AUDIT));
        return auditService.audit(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.HELP, AllAuditActionStatusEnum.AUDIT));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean refuse(SpAuditDTO dto) {
        SpHelpResultDTO old = Optional.ofNullable(helpService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        helpService.changeAction(new SpBriefPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), BriefAuditActionStatusEnum.REFUSE));
        return auditService.refuse(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.HELP, AllAuditActionStatusEnum.REFUSE));
    }
}
